package com.example.travel.service.impl;

import com.example.travel.entity.Hotel;
import com.example.travel.entity.Orderform;
import com.example.travel.entity.bo.OrderformBO;
import com.example.travel.entity.dto.HotelDTO;
import com.example.travel.entity.dto.UserDTO;
import com.example.travel.mapper.HotelMapper;
import com.example.travel.service.IHotelService;
import com.example.travel.util.OrderNumberUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HotelService implements IHotelService {

    @Autowired
    private HotelMapper hotelMapper;

    @Override
    public PageInfo<Hotel> queryHotelInfo(HotelDTO hotelDTO) {
        PageHelper.startPage(hotelDTO.getPageNum(), hotelDTO.getPageSize());
        List<Hotel> list = hotelMapper.queryHotelInfo(hotelDTO);
        return new PageInfo<>(list);
    }

    @Override
    public boolean userBookHotel(Orderform orderform) {

        orderform.setOrderNumber(OrderNumberUtil.getOrderNumber());
        boolean orderForm = hotelMapper.userBookHotel(orderform);
        if (orderForm) {
//            生成订单成功 修改酒店房间库存
            hotelMapper.updateHotelInventory(orderform);
            return true;
        }
        return false;
    }

    @Override
    public PageInfo<OrderformBO> queryOrderForm(UserDTO userDTO) {
        PageHelper.startPage(userDTO.getPageNum(), userDTO.getPageSize());
        List<OrderformBO> list = hotelMapper.queryOrderForm(userDTO);
        return new PageInfo<>(list);
    }
}
